#include "main.h"

void* tTest1(void* arg)
{
	int n = 100;
	int count = 10;
	int count_sum;
	float* a;
	float* b;
	int i, j, k;
	float Output_Value = 0;
	struct timespec time0, time1;

	time0 = RealTime_Get();
	time1 = RealTime_Get();
	
	while (1)
	{
		//time0 = RealTime_Get();											
		////< printf("dt1 = %d\r\n", Dt_Calu(time0, time1));				//< duan0920 完成10个一百万次乘法运算所需要的时间			
		//time1 = time0;
		//for (count_sum = 0; count_sum < count; count_sum++)				
		//{
		//	a = (float*)calloc(n, sizeof(float));						//< duan0920 开辟内存
		//	b = (float*)calloc(n, sizeof(float));

		//	for (i = 0; i < n; i++)										//< duan0920 赋值
		//	{
		//		a[i] = ((float)rand()) / ((float)RAND_MAX);
		//		b[i] = ((float)rand()) / ((float)RAND_MAX);
		//	}

		//	Output_Value = 0;
		//	for (i = 0; i < n; i++)										//< duan0920 执行一百万次乘法运算
		//	{
		//		for (j = 0; j < n; j++)
		//		{
		//			for (k = 0; k < n; k++)
		//			{
		//				Output_Value += a[k] * b[k];
		//			}
		//		}
		//	}

		//	free(a);													//< duan0920 释放内存
		//	free(b);
		//}
		//ExactDelay(20);
	}
}

void* tTest2(void* arg)
{
	int n = 100;
	int count = 10;
	int count_sum;
	float* a;
	float* b;
	int i, j, k;
	float Output_Value = 0;
	struct timespec time0, time1;

	time0 = RealTime_Get();
	time1 = RealTime_Get();

	while (1)
	{
		time0 = RealTime_Get();
		//printf("dt2 = %d\r\n", Dt_Calu(time0, time1));
		time1 = time0;
		for (count_sum = 0; count_sum < count; count_sum++)
		{
			a = (float*)calloc(n, sizeof(float));
			b = (float*)calloc(n, sizeof(float));

			for (i = 0; i < n; i++)
			{
				a[i] = ((float)rand()) / ((float)RAND_MAX);
				b[i] = ((float)rand()) / ((float)RAND_MAX);
			}

			Output_Value = 0;
			for (i = 0; i < n; i++)
			{
				for (j = 0; j < n; j++)
				{
					for (k = 0; k < n; k++)
					{
						Output_Value += a[k] * b[k];
					}
				}
			}

			free(a);
			free(b);
		}
		ExactDelay(20);
	}
}

void* tTest3(void* arg)
{
	int n = 100;
	int count = 10;
	int count_sum;
	float* a;
	float* b;
	int i, j, k;
	float Output_Value = 0;
	struct timespec time0, time1;

	time0 = RealTime_Get();
	time1 = RealTime_Get();

	while (1)
	{
		time0 = RealTime_Get();
		//< printf("dt3 = %d\r\n", Dt_Calu(time0, time1));
		time1 = time0;
		for (count_sum = 0; count_sum < count; count_sum++)
		{
			a = (float*)calloc(n, sizeof(float));
			b = (float*)calloc(n, sizeof(float));

			for (i = 0; i < n; i++)
			{
				a[i] = ((float)rand()) / ((float)RAND_MAX);
				b[i] = ((float)rand()) / ((float)RAND_MAX);
			}

			Output_Value = 0;
			for (i = 0; i < n; i++)
			{
				for (j = 0; j < n; j++)
				{
					for (k = 0; k < n; k++)
					{
						Output_Value += a[k] * b[k];
					}
				}
			}

			free(a);
			free(b);
		}
		ExactDelay(20);
	}
}

//************************************
// Method:    	main							主函数32100123
// Programmer:  lww
// Time:		2019/2/16
// Returns:   	int
//************************************
int main()
{
	pthread_t tPSSwitch_ID = 0;
	pthread_t tBCDPUStatus_ID = 0;
	pthread_t tBCNICStatus_ID = 0;
	pthread_t tDown_ID = 0;
	pthread_t tReadCard_ID = 0;
	pthread_t tCalcu_ID = 0;
	pthread_t tBcNetIOAndRecvBc_ID = 0;
	pthread_t tBCIO_ID = 0;
	pthread_t tBCSAMAIO_ID = 0;
	pthread_t tBCSAMA_ID = 0;
	pthread_t tBCSAMAPARA_ID = 0;
	pthread_t tRecvCMD_ID = 0;
	pthread_t tRecvDCSBC_ID = 0;
	pthread_t tBakAndRecvBak_ID = 0;
	pthread_t tBakIOAndRecvBak_ID = 0;
	pthread_t tSetTime_ID = 0;
	pthread_t tReadSamaIO_ID = 0;
	pthread_t tTest1_ID = 0;
	pthread_t tTest2_ID = 0;
	pthread_t tTest3_ID = 0;	
	
	if (false == System_Initialization()) { perror("Init system failed\n"); return 0; }																			//< 系统初始化

	//void* handle = NULL;
	//char* error;
	//int (*CAC_FUNC)(int*, int);
	//int c;
	//int aa[3] = { 10, 20, 30 };
	//handle = dlopen("/home/lww/myso/libTest_So.so", RTLD_LAZY);
	//if (handle == NULL) 
	//{
	//	printf("%s\r\n", dlerror());
	//	return 0;
	//}
	//dlerror();
	//CAC_FUNC = (int (*)(int*, int))dlsym(handle, "add");
	//if ((error = dlerror()) != NULL) 
	//{
	//	printf("%s\r\n", error);
	//	return 0;
	//}
	//c = CAC_FUNC(aa, 3);
//	/****************************************/
//	/////////////*python参数定义*/////////////
//	/****************************************/
//#define SOFILE_PATH	"/home/lww/myso/"
//	PyObject* pName = NULL;
//	PyObject* pModule = NULL;
//	PyObject* pDict = NULL;
//	PyObject* pFunc = NULL;
//	PyObject* pArgs = NULL;
//	PyObject* pArgs_Return = NULL;
//	PyObject* pAI_List = NULL;
//	PyObject* pDI_List = NULL;
//	PyObject* pAO_List = NULL;
//	PyObject* pDO_List = NULL;
//	PyObject* pAnaPara_List = NULL;
//	PyObject* pDigPara_List = NULL;
//	PyObject* pAnaSta_List = NULL;
//	PyObject* pDigSta_List = NULL;
//	int DINum = 2;
//	int DONum = 3;
//	int AINum = 4;
//	int AONum = 5;
//	int AnaParaNum = 6;
//	int DigParaNum = 7;
//	int AnaStaNum = 2;
//	int DigStaNum = 2;
//	bool DI[2] = { true, false };
//	bool DO[3] = { false, true, false };
//	float AI[4] = { 0.1, 0.2, 0.3, 0.4 };
//	float AO[5] = { 0.5, 0.6, 0.7, 0.8, 0.9 };
//	float AnaPara[6] = { 1.0, 1.1, 1.2, 1.3, 1.4, 1.5 };
//	float dt = 0.2;
//	bool DigPara[7] = { true, true, false, true, false, true, true };
//	bool DigSta[2] = { true, false };
//	float AnaSta[2] = { 12.13, 13.14 };
//
//	char So_Name[5] = "USER";
//	char So_Path[100] = { 0 };
//	int i;
//	/****************************************/
//	///////////*python参数定义结束*///////////
//	/****************************************/
//
//	/****************************************/
//	//////////*python文件及函数调用*//////////
//	/****************************************/
//	Py_Initialize();							//< 初始化Python，它会载入Python的内建模块并添加系统路径到模块搜索路径中。这个函数没有返回值，检查系统是否初始化成功需要使用Py_IsInitialized
//	if (!Py_IsInitialized()) { return 0; }		//< 检查初始化是否成功 
//	//< 添加当前路径，把输入的字符串作为Python代码直接运行，返回0表示成功，-1表示有错，大多时候错误都是因为字符串中有语法错误
//	PyRun_SimpleString("import sys");
//	PyRun_SimpleString("print '---import sys---'");
//	strcpy(So_Path, "sys.path.append('");
//	strcat(So_Path, SOFILE_PATH);
//	strcat(So_Path, "')");
//	PyRun_SimpleString(So_Path);
//	pName = PyString_FromString(So_Name);
//	pModule = PyImport_Import(pName);
//	if (!pModule) { printf("can't find %s.py\r\n", So_Name); return 0; }
//	pDict = PyModule_GetDict(pModule);
//	if (!pDict) { return 0; }
//
//	pFunc = PyDict_GetItemString(pDict, "Calcu");// 找出函数名为Calcu的函数
//	if (!pFunc || !PyCallable_Check(pFunc)) { printf("can't find function\r\n"); return 0; }
//	/****************************************/
//	////////*python文件及函数调用结束*////////
//	/****************************************/
//
//	/****************************************/
//	////////////////*参数进栈*////////////////
//	/****************************************/
//	pArgs = PyTuple_New(9);					//< 共7个参数存放在一个元组中，其中6个为list，一个为float
//	pAI_List = PyList_New(0);
//	pAO_List = PyList_New(0);
//	pDI_List = PyList_New(0);
//	pDO_List = PyList_New(0);
//	pAnaPara_List = PyList_New(0);
//	pDigPara_List = PyList_New(0);
//	pAnaSta_List = PyList_New(0);
//	pDigSta_List = PyList_New(0);
//
//	//< 参数一赋值
//	for (i = 0; i < DINum; i++)
//	{
//		PyList_Append(pDI_List, Py_BuildValue("b", DI[i]));
//	}
//	PyTuple_SetItem(pArgs, 0, pDI_List);
//	//< 参数二赋值
//	for (i = 0; i < DONum; i++)
//	{
//		PyList_Append(pDO_List, Py_BuildValue("b", DO[i]));
//	}
//	PyTuple_SetItem(pArgs, 1, pDO_List);
//	//< 参数三赋值
//	for (i = 0; i < AINum; i++)
//	{
//		PyList_Append(pAI_List, Py_BuildValue("f", AI[i]));
//	}
//	PyTuple_SetItem(pArgs, 2, pAI_List);
//	//< 参数四赋值
//	for (i = 0; i < AONum; i++)
//	{
//		PyList_Append(pAO_List, Py_BuildValue("f", AO[i]));
//	}
//	PyTuple_SetItem(pArgs, 3, pAO_List);
//	//< 参数五赋值
//	PyTuple_SetItem(pArgs, 4, Py_BuildValue("f", dt));
//	//< 参数六赋值
//	for (i = 0; i < DigParaNum; i++)
//	{
//		PyList_Append(pDigPara_List, Py_BuildValue("b", DigPara[i]));
//	}
//	PyTuple_SetItem(pArgs, 5, pDigPara_List);
//	//< 参数七赋值
//	for (i = 0; i < AnaParaNum; i++)
//	{
//		PyList_Append(pAnaPara_List, Py_BuildValue("f", AnaPara[i]));
//	}
//	PyTuple_SetItem(pArgs, 6, pAnaPara_List);
//	//< 参数八赋值
//	for (i = 0; i < DigStaNum; i++)
//	{
//		PyList_Append(pDigSta_List, Py_BuildValue("b", DigSta[i]));
//	}
//	PyTuple_SetItem(pArgs, 7, pDigSta_List);
//	//< 参数九赋值
//	for (i = 0; i < AnaStaNum; i++)
//	{
//		PyList_Append(pAnaSta_List, Py_BuildValue("f", AnaSta[i]));
//	}
//	PyTuple_SetItem(pArgs, 8, pAnaSta_List);
//	/****************************************/
//	//////////////*参数进栈结束*//////////////
//	/****************************************/
//
//	/****************************************/
//	////////////////*执行函数*////////////////
//	/****************************************/
//	pArgs_Return = PyObject_CallObject(pFunc, pArgs);
//	/****************************************/
//	//////////////*执行函数结束*//////////////
//	/****************************************/
//
//	/****************************************/
//	//////////////*获取运算结果*//////////////
//	/****************************************/
//	for (i = 0; i < DONum; i++)
//	{
//		DO[i] = ((PyInt_AsLong(PyList_GetItem(PyTuple_GetItem(pArgs_Return, 0), i)) == 1) ? true : false);
//	}
//	for (i = 0; i < AONum; i++)
//	{
//		AO[i] = (float)PyFloat_AsDouble(PyList_GetItem(PyTuple_GetItem(pArgs_Return, 1), i));
//	}
//	for (i = 0; i < DigStaNum; i++)
//	{
//		DigSta[i] = ((PyInt_AsLong(PyList_GetItem(PyTuple_GetItem(pArgs_Return, 2), i)) == 1) ? true : false);
//	}
//	for (i = 0; i < AnaStaNum; i++)
//	{
//		AnaSta[i] = (float)PyFloat_AsDouble(PyList_GetItem(PyTuple_GetItem(pArgs_Return, 3), i));
//	}
//	/****************************************/
//	////////////*获取运算结果结束*////////////
//	/****************************************/
//
//	/****************************************/
//	////////////////*释放内存*////////////////
//	/****************************************/
//	Py_DECREF(pAI_List);
//	Py_DECREF(pAO_List);
//	Py_DECREF(pDI_List);
//	Py_DECREF(pDO_List);
//	Py_DECREF(pAnaPara_List);
//	Py_DECREF(pDigPara_List);
//	Py_DECREF(pAnaSta_List);
//	Py_DECREF(pDigSta_List);
//	Py_DECREF(pArgs);
//	Py_DECREF(pArgs_Return);
//	/****************************************/
//	//////////////*释放内存结束*//////////////
//	/****************************************/
	PowerOnDeleteAllFile();
	//THREAD_CREATE_FAILED(Thread_Create("tSetTime_ID", &tSetTime_ID, NULL, SCHED_POLICY, 89, true, tSetTime, CONTROL_CORE, NULL));								//< 对时功能//储能项目控制器直接北斗定时
	THREAD_CREATE_FAILED(Thread_Create("tPSSwitch_ID", &tPSSwitch_ID, NULL, SCHED_POLICY, 89, true, tPSSwitch, CONTROL_CORE, NULL));							//< 主站或从站状态发送+主从站身份判断线程
	while (SYS_Role == Default_Role); { ExactDelay(100); }																										//< 当系统角色为未定义时，循环等待系统角色确定
	if (false == File_Manage_Func()) { perror("File manage failed\n"); return 0; }																				//< 系统文件管理
	THREAD_CREATE_FAILED(Thread_Create("tBCDPUStatus_ID", &tBCDPUStatus_ID, NULL, SCHED_POLICY, 89, true, tBCDPUStatus, CONTROL_CORE, NULL));					//< 开启广播DPU状态线程
	//THREAD_CREATE_FAILED(Thread_Create("tBCNICStatus_ID", &tBCNICStatus_ID, NULL, SCHED_POLICY, 89, true, tBCNICStatus, CONTROL_CORE, NULL));			        //< 广播网卡状态线程（104通讯网卡，储能用）

	THREAD_CREATE_FAILED(Thread_Create("tRecvDCSBC_ID", &tRecvDCSBC_ID, NULL, SCHED_POLICY, 89, true, tRecvDCSBC, CONTROL_CORE, NULL));							//< 开启接收DCS状态广播线程
	THREAD_CREATE_FAILED(Thread_Create("tDown_ID", &tDown_ID, NULL, SCHED_POLICY, 89, true, tDown, CONTROL_CORE, NULL));										//< 下装/在线下装线程 （主站下装，再将主站的文件发到从站同样的路径下）
	RoughDelay_S(5);		
	while (!SYS_ReadCard_Start) { ExactDelay(100); }
	THREAD_CREATE_FAILED(Thread_Create("tReadCard_ID", &tReadCard_ID, NULL, SCHED_POLICY, 89, true, tReadCard, CONTROL_CORE, NULL));							//< 读卡线程									
	while (!SYS_Calcu_Start); { ExactDelay(100); }
	THREAD_CREATE_FAILED(Thread_Create("tCalcu_ID", &tCalcu_ID, NULL, SCHED_POLICY, 89, true, tCalcu, CONTROL_CORE, NULL));										//< 运算线程									
	//< CARD_WORK(SAMAIO)(); 
	ExactDelay(2000);
	while (!SYS_BcORBak_Start) { ExactDelay(100); }
	THREAD_CREATE_FAILED(Thread_Create("tReadSamaIO_ID", &tReadSamaIO_ID, NULL, SCHED_POLICY, 89, true, tReadSamaIO, CONTROL_CORE, NULL));						//< 读取 SAMA 软点值（A0或DO）线程	     （SAMAIO通道值写入数据库中）  
	THREAD_CREATE_FAILED(Thread_Create("tBcNetIOAndRecvBc_ID", &tBcNetIOAndRecvBc_ID, NULL, SCHED_POLICY, 89, true, tBcNetIOAndRecvBc, CONTROL_CORE, NULL));	//< 站间引用点广播以及接受站间引用点广播线程		
	THREAD_CREATE_FAILED(Thread_Create("tBCIO_ID", &tBCIO_ID, NULL, SCHED_POLICY, 89, true, tBCIO, CONTROL_CORE, NULL));										//< 广播IO硬点状态线程		//< lijun20220811 优先级由89改为90
	THREAD_CREATE_FAILED(Thread_Create("tBCSAMAIO_ID", &tBCSAMAIO_ID, NULL, SCHED_POLICY, 89, true, tBCSAMAIO, CONTROL_CORE, NULL));							//< 广播IO软点状态线程							
	THREAD_CREATE_FAILED(Thread_Create("tBCSAMA_ID", &tBCSAMA_ID, NULL, SCHED_POLICY, 89, true, tBCSAMA, CONTROL_CORE, NULL));									//< 广播SAMA状态线程
	THREAD_CREATE_FAILED(Thread_Create("tBCSAMAPARA_ID", &tBCSAMAPARA_ID, NULL, SCHED_POLICY, 89, true, tBCSAMAPARA, CONTROL_CORE, NULL));						//< 广播SAMA模块参数线程
	THREAD_CREATE_FAILED(Thread_Create("tBakAndRecvBak_ID", &tBakAndRecvBak_ID, NULL, SCHED_POLICY, 89, true, tBakAndRecvBak, CONTROL_CORE, NULL));				//< 定时向从站备份运算数据或从站解析主站备份的运算数据
	THREAD_CREATE_FAILED(Thread_Create("tBakIOAndRecvBak_ID", &tBakIOAndRecvBak_ID, NULL, SCHED_POLICY, 89, true, tBakIOAndRecvBak, CONTROL_CORE, NULL));		//< 主站定期向从站备份IO硬点据或从站接收备份数据的线程
	THREAD_CREATE_FAILED(Thread_Create("tRecvCMD_ID", &tRecvCMD_ID, NULL, SCHED_POLICY, 89, true, tRecvCMD, CONTROL_CORE, NULL));								//< 接受上位来的命令并作出相应处理

//	THREAD_CREATE_FAILED(Thread_Create("tTest1_ID", &tTest1_ID, NULL, SCHED_POLICY, 89, true, tTest1, CPU_CORE1, NULL));
//	THREAD_CREATE_FAILED(Thread_Create("tTest2_ID", &tTest2_ID, NULL, SCHED_POLICY, 89, true, tTest2, CPU_CORE2, NULL));
//	THREAD_CREATE_FAILED(Thread_Create("tTest3_ID", &tTest3_ID, NULL, SCHED_POLICY, 89, true, tTest3, CPU_CORE3, NULL));
	
	

	while (1) {
		RoughDelay_S(10000);			
	}
	return 0;
}

/*系统初始化函数*/
//************************************
// Method:    	System_Initialization			系统初始化函数
// Programmer:  lww
// Time:		2018/11/12
// Returns:   	bool							true——初始化成功；false——初始化失败
//************************************
bool System_Initialization()
{
	void* IPAddr_Start = NULL;	//< 存放文件内容的内存的首地址
	void* IPAddr = NULL;		//< 当前读写文件位置在内存中的地址
	void* Gateway_Start = NULL;
	void* Gateway = NULL;
	int File_Size = 0;			//< 文件字节数
	char IP_A[20] = { 0 };
	char IP_B[20] = { 0 };
	//< 初始化系统硬件调用

	//////读串口配置文件，取出IP
	/*char* ETH0[10] = {};
	char aa[17] = "192.168.4.10\n";
	Open2Read2(ETH0_CONFIGURATION, ETH0);
	for (int i=0;i<5;i++)
	{
		if (strcmp(ETH0[i], aa) == 0)		
		{
			continue;
		}
	}*/


////↓↓↓↓↓↓↓↓***************功率预测文件读取到数组测试**************************↓↓↓↓↓↓↓↓
//需要包含File_Deal.h文件
//#include "Global_Func/POSIX_Time/POSIX_Time.h"
//#include "Global_Func/File/File_Deal.h"	//< 自写文件处理函数头文件
//#define PREDICTION_NUM_MAX_Short 96  //输出float类型数组的最大长度(短期)
//#define PREDICTION_NUM_MAX_Ultra_Short 16  //输出float类型数组的最大长度(超短期)
//#define SPREDICTION_CONTENT_MAX 18432  //存储文件内容char数组的最大长度
//#define USPREDICTION_CONTENT_MAX 2048  //存储文件内容char数组的最大长度
//
//#define SNeedNum 96/*要取点的数量*/
//#define USNeedNum 16/*要取点的数量*/
//	char SFileContent[SPREDICTION_CONTENT_MAX] = {};
//	char USFileContent[USPREDICTION_CONTENT_MAX] = {};
//
//	char SNum[4] = {}; char SNum_Next[4] = {};
//	char USNum[4] = {}; char USNum_Next[4] = {};
//
//	void* SFileContent_Start = NULL;	/*存放文件内容的内存的首地址*/void* SpcFileContent = NULL;/*当前读写文件位置在内存中的地址*/int SFile_Size = 0;/* 文件字节数*/
//	void* USFileContent_Start = NULL;	/*存放文件内容的内存的首地址*/void* USpcFileContent = NULL;/*当前读写文件位置在内存中的地址*/int USFile_Size = 0;/* 文件字节数*/
//
//	float SForecast[PREDICTION_NUM_MAX_Short] = {};
//	float USForecast[PREDICTION_NUM_MAX_Ultra_Short] = {};
//	
//	
//	char space[2] = " ";
//	//int TimeDelayMin = 15;  //每15min读一次文件
//
//	int todayday = 0; int tomorrowday = 0; int todaymonth = 0; int tomorrowmonth = 0; int todayyear = 0; int tomorrowyear = 0;
//	int NowMin = 0; int NowHour = 0; int UltraShortMin = 0; int UltraShortHour = 0;
//	time_t time_Lf = time(NULL);
//	struct tm*tm_Lf = localtime(&time_Lf);
//	todayyear = tm_Lf->tm_year + 1900; todaymonth = tm_Lf->tm_mon + 1; todayday = tm_Lf->tm_mday;//当天的年月日
//	time_t time_Lf_tomorrow = time(NULL) + 86400;//一天后的时间
//	struct tm*tm_Lf_tomorrow = localtime(&time_Lf_tomorrow);
//	tomorrowyear = tm_Lf_tomorrow->tm_year + 1900; tomorrowmonth = tm_Lf_tomorrow->tm_mon + 1; tomorrowday = tm_Lf_tomorrow->tm_mday;//明天的年月日
//	NowHour = tm_Lf_tomorrow->tm_hour; NowMin = tm_Lf_tomorrow->tm_min;//当前时分
//
//	 
//	char ShortPath[65] = LoadForecastShortFile_PATH_HEAD;
//	char UltraShortPath[65] = LoadForecastUltraShortFile_PATH_HEAD;
//	char ShortDate[10] = {};
//	char UltraShortDate[10] = {};
//	char UltraShortTime[10] = {};
//	sprintf(ShortDate, "%04d%02d%02d", tomorrowyear, tomorrowmonth, tomorrowday);
//	if (NowHour == 23 && NowMin > 45) {
//		sprintf(UltraShortDate, "%04d%02d%02d", tomorrowyear, tomorrowmonth, tomorrowday);
//	}
//	else { sprintf(UltraShortDate, "%04d%02d%02d", todayyear, todaymonth, todayday); }
//	if (NowMin>=0&& NowMin <15){
//		UltraShortMin = 15;
//		UltraShortHour = NowHour;
//	}
//	else if (NowMin >= 15 && NowMin < 30) {
//		UltraShortMin = 30;
//		UltraShortHour = NowHour;
//	}
//	else if (NowMin >= 30 && NowMin < 45) {
//		UltraShortMin = 45;
//		UltraShortHour = NowHour;
//	}
//	else if (NowMin >= 45 && NowHour!=23) {
//		UltraShortMin = 0;
//		UltraShortHour = NowHour+1;
//	}
//	else if (NowMin >= 45 && NowHour == 23) {
//		UltraShortMin = 0;
//		UltraShortHour = 0;
//	}
//	sprintf(UltraShortTime, "%02d%02d",UltraShortHour, UltraShortMin);
//
//	strcat(ShortPath, ShortDate); strcat(ShortPath, LoadForecastShortFile_PATH_TAIL);//形成短期预测文件的完整路径
//	strcat(UltraShortPath, UltraShortDate); strcat(UltraShortPath, UnderLine);                           //形成超短期预测文件的完整路径
//	strcat(UltraShortPath, UltraShortTime); strcat(UltraShortPath, LoadForecastUltraShortFile_PATH_TAIL);//形成超短期预测文件的完整路径
//		
//
//	int Snumcount = 0;/*计数变量*/
//	if (Open2Read(ShortPath, &SFileContent_Start, &SpcFileContent, &SFile_Size) == false) {	//< 打开存储IP地址的文件并读取IP地址
//		/*printf("Open %s failed\r\n", ETH0_CONFIGURATION);*/ goto SReadFileFailed;//若读取文件失败则报错
//	}
//	strcpy(SFileContent, (const char*)SpcFileContent);//将文件内容复制到FileContent中
//	while (Snumcount <= SNeedNum) {                   //当数据计数小于所需数据点数
//		char SNum_char[15] = {};      //存放字符型数值#n的变量
//		char SNum_char_temp[15] = {};      //存放字符型数值#n的变量
//		char Source1[47] = "t::ZDDYFD>\r\n@id";
//		char Source[6] = "#";
//		char Source_Next[6] = "#";
//		char *pcBegin = NULL;
//		char *pcEnd = NULL;
//		Snumcount++;
//		int numcount_next = Snumcount + 1;
//		sprintf(SNum, "%d", Snumcount);            //将int型numcount转为字符型Num
//		sprintf(SNum_Next, "%d", numcount_next);  //将int型numcount_next转为字符型Num_Next
//		strcat(Source, SNum);                //拼接“#”与Num ，     形成#n字符，存放在Source中
//		strcat(Source_Next, SNum_Next);      //拼接“#”与Num_Next ，形成#n字符，存放在Source_Next中
//		if (Snumcount == 1) {
//			pcBegin = strstr(SFileContent, Source1);
//		}
//		else {
//			pcBegin = strstr(SFileContent, Source);
//		}
//		pcEnd = strstr(SFileContent, Source_Next)-2;
//		if (pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd) {
//			printf("Keywords not found!\n");
//		}
//		else {
//			if (Snumcount == 1) {
//				pcBegin += 42;
//			} 
//			else {
//				pcBegin += strlen(Source)+1;//需测试
//			}
//			memcpy(SNum_char_temp, pcBegin, pcEnd - pcBegin);
//			for (int i = 0; i < 16; i++) {
//				if (SNum_char_temp[i]!= space[0]) {
//					SNum_char[i] = SNum_char_temp[i];
//				}
//				else {
//					i = 16;
//				}
//			}
//		}
//		SForecast[Snumcount - 1] = atof(SNum_char);                    //将字符串cRes转换为浮点数存到Forecast[numcount - 1]中去
//	}
//	Del_File(ShortPath);
//
//SReadFileFailed:
//	int USnumcount = 0;/*计数变量*/
//	if (Open2Read(UltraShortPath, &USFileContent_Start, &USpcFileContent, &USFile_Size) == false) {	//< 打开存储IP地址的文件并读取IP地址
//		 goto USReadFileFailed;//若读取文件失败则报错
//	}
//	strcpy(USFileContent, (const char*)USpcFileContent);//将文件内容复制到FileContent中
//	while (USnumcount <= USNeedNum) {                   //当数据计数小于所需数据点数
//		char USNum_char[15] = {};      //存放字符型数值#n的变量
//		char Source1[47] = "t::ZDDYFD>\r\n@id";
//		char Source[6] = "#";
//		char Source_Next[6] = "#";
//		char *pcBegin = NULL;
//		char *pcEnd = NULL;
//		USnumcount++;
//		int numcount_next = USnumcount + 1;
//		sprintf(USNum, "%d", USnumcount);            //将int型numcount转为字符型Num
//		sprintf(USNum_Next, "%d", numcount_next);  //将int型numcount_next转为字符型Num_Next
//		strcat(Source, USNum);                //拼接“#”与Num ，     形成#n字符，存放在Source中
//		strcat(Source_Next, USNum_Next);      //拼接“#”与Num_Next ，形成#n字符，存放在Source_Next中
//		if (USnumcount==1){
//			pcBegin= strstr(USFileContent, Source1);
//		}
//		else {
//			pcBegin = strstr(USFileContent, Source);
//		}
//		pcEnd = strstr(USFileContent, Source_Next);
//		if (pcBegin == NULL || pcEnd == NULL || pcBegin > pcEnd) {
//			printf("Keywords not found!\n");
//		}
//		else {
//			if (USnumcount == 1) {
//				pcBegin +=34;
//			}
//			else {
//				pcBegin += strlen(Source);
//			}
//			memcpy(USNum_char, pcBegin, pcEnd - pcBegin);
//		}
//		USForecast[USnumcount - 1] = atof(USNum_char);                    //将字符串cRes转换为浮点数存到Forecast[numcount - 1]中去
//	}
//	Del_File(UltraShortPath);
//USReadFileFailed:
//
////↑↑↑↑↑↑↑↑***************功率预测文件读取到数组测试**************************↑↑↑↑↑↑↑↑



	//< duan0529 避免串口冲突
#if PRINTF_REPOINT
	if (false == Printf_Repoint())		//< 实现printf重定向到串口
		return false;
#endif

	//< 初始化IP地址最后一位
	if (START_ON_LOCAL == true)
	{
		if (Open2Read(IP_FILE_PATH, &IPAddr_Start, &IPAddr, &File_Size) == false)	//< 打开存储IP地址的文件并读取IP地址
		{
			printf("Open %s failed\r\n", IP_FILE_PATH);
			return false;
		}
		IPTail_Num = IPTail_Num_Get((char*)IPAddr);		//IPAddr:工程机主站IP->192.166.100.16   工程机备站IP->192.166.100.66 
	}
	else
		IPTail_Num = STATION_IP_TAIL_NUM;

	//< 设置网卡IP
	//IP_Get(IP_A, IPTail_Num, NETA);
	//if (SetLocal_IP(NETWORK_CARD_A, IP_A) == false)						//< 设置IP地址
	//	return false;
	//if (Open2Read(GATEWAY_FILE_PATH, &Gateway_Start, &Gateway, &File_Size) == true)	//< 如果没有打开设置网关的文件，那么就不设置
	//{
	//	SetLocal_Route(NETWORK_CARD_A, (char*)Gateway);					//< Shike20210513 设置网关的函数，实验发现好像设置网关只能第一次成功，所以要VS远程调试的话需要注释掉或者删掉gateway.txt，必须使用的话只能重新开机再远程调试
	//	//if (SetLocal_Route(NETWORK_CARD_A, (char*)Gateway) == false)
	//	//return false;
	//}

#ifdef IS_USE_LANB
	IP_Get(IP_B, IPTail_Num, NETB);
	if (SetLocal_IP(NETWORK_CARD_B, IP_B) == false)						//< 设置IP地址
		return false;
#endif

	//< 初始化站号
	if (IPTail_Num < MASTER_SLAVE_ID_DT)
		Station_ID = IPTail_Num;
	else
		Station_ID = (unsigned char)(IPTail_Num - MASTER_SLAVE_ID_DT);//站号保持是主站号

	//< 初始化系统信号量
	/*MUTEX_INIT(BakData_Mutex, "BakData_Mutex init failed");
	MUTEX_INIT(BakIO_Mutex, "BakIO_Mutex init failed");
	MUTEX_INIT(BcDPU_Mutex, "BcDPU_Mutex init failed");
	MUTEX_INIT(BcAVIO_Mutex, "BcAVIO_Mutex init failed");
	MUTEX_INIT(BcDVIO_Mutex, "BcDVIO_Mutex init failed");
	MUTEX_INIT(BcSama_Mutex, "BcSama_Mutex init failed");
	MUTEX_INIT(BcSamaPara_Mutex, "BcSamaPara_Mutex init failed");
	MUTEX_INIT(BcSamaIO_Mutex, "BcSamaIO_Mutex init failed");
	MUTEX_INIT(ReadSamaIO_Mutex, "ReadSamaIO_Mutex init failed");
	MUTEX_INIT(BcNetIO_Mutex, "BcNetIO_Mutex init failed");
	MUTEX_INIT(Calcu_Mutex, "Calcu_Mutex init failed");*/

	//< duan20200205修改
	//< 多线程中定时的信号量
	SEM_OPEN(BakDataSemId, "/BakData_SemId", "BakDataSemId open failed", 0);
	SEM_OPEN(BakIOSemId, "/BakIO_SemId", "BakIOSemId open failed", 0);
	SEM_OPEN(BcDPUSemId, "/BcDPU_SemId", "BcDPUSemId open failed", 0);
	SEM_OPEN(BcAVIOSemId, "/BcIO_SemId", "BcAVIOSemId open failed", 0);
	SEM_OPEN(BcDVIOSemId, "/BcIO_SemId", "BcDVIOSemId open failed", 0);
	SEM_OPEN(BcSamaSemId, "/BcSama_SemId", "BcSamaSemId open failed", 0);
	SEM_OPEN(BcSamaParaSemId, "/BcSamaPara_SemId", "BcSamaParaSemId open failed", 0);
	SEM_OPEN(BcSamaIOSemId, "/BcSamaIO_SemId", "BcSamaIOSemId open failed", 0);
	SEM_OPEN(ReadSamaIOSemId, "/ReadSamaIOSemId", "ReadSamaIOSemId open failed", 0);
	SEM_OPEN(BcNetIOSemId, "/BcNetIO_SemId", "BcNetIOSemId open failed", 0);
	SEM_OPEN(CalcuSemId, "/Calcu_SemId", "CalcuSemId open failed", 0);


	//< 初始化全局变量
	SYS_Role_Turn(Default_Role);				//< 初始化系统角色为未定义
	SYS_Status_Turn(Default_Status);			//< 初始化系统状态为离线
	SYS_NetA_Status = Net_Trouble;				//< 初始化A网段状态为故障
	SYS_NetB_Status = Net_Trouble;				//< 初始化B网段状态为故障
	SYS_NetD_Status = Net_Trouble;				//< 初始化D网段状态为故障(104通讯网段状态)

	SYS_ReadCard_Start = false;					//< 开始执行读卡线程标志为false
	SYS_Calcu_Start = false;					//< 开始执行运算线程标志为false
	SYS_BcORBak_Start = false;					//< 开始执行广播及备份线程标志为false
	SYS_BaseTime = 0;							//< 站基准周期，由上位下发得到

	/*getpid: 用来取得目前进程的进程识别码，许多程序利用取到的此值来建立临时文件，以避免临时文件相同带来的问题。
	  返回值： 目前进程的进程识别码
	*/
	printf("the PROCESS_ID is %d\r\n", getpid());										//< duan20200610
	return true;
}